<?php

// MODELE ARTICLE

abstract class Modele {

    protected $mysql;

    public function __construct() {
        try {

            $this->mysql = new PDO(SERVER, USER, PASSWORD);
        } catch (PDOException $e) {
            echo"Erreur de connexion au serveur :<br>" . $e->getMessage() . "<br>";
        }
    }

}

class ModeleArticles extends Modele {

    // METHODE AFFICHAGE ARTICLE

    public function selectListeArticle($articles, $contenu) {

        $sql = "SELECT $contenu FROM $articles ORDER BY dateHeure DESC";

        $req = $this->mysql->query($sql);

        return $req->fetchAll();
    }

    public function selectUnArticle($articles, $contenu, $id) {

        $sql = "SELECT $contenu FROM $articles WHERE ID=$id";

        $req = $this->mysql->query($sql);

        return $req->fetchAll();
    }

    public function UpdateUnArticle($table, $id, $tab) {

        $temp = null;

        for ($x = 0; $x < count($tab); $x++) {
            $temp .= $tab[$x][0] . "='" . $tab[$x][1] . "'";
            if ($x < (count($tab) - 1))
                $temp.=", ";
        }
        $sql = "UPDATE $table SET $temp WHERE ID=$id";

        $req = $this->mysql->query($sql);
    }

    // METHODE SUPPRESSION ARTICLE

    public function supprimerArticle($articles, $ID) {

        $idArticle = $_GET["idArticle"];
        $sql = "DELETE FROM $articles WHERE $ID=$idArticle";

        $req = $this->mysql->query($sql);
    }

}
